/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.DragonPokerServer.Thread;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import org.DragonPokerServer.dbclasses.User;
import org.Comunicator.Message;
import org.DragonPokerServer.Thread.ServerRequest.CreateRoomRequest;
import org.DragonPokerServer.Thread.ServerRequest.GenericServerRequest;
import org.DragonPokerServer.Thread.ServerRequest.LoginRequest;
import org.DragonPokerServer.Thread.ServerRequest.PlayersInRoomRequest;
import org.DragonPokerServer.Thread.ServerRequest.RegisterRequest;
import org.DragonPokerServer.Thread.ServerRequest.RoomsRequest;

/**
 *
 * @author gonçalo.margalho
 */
public class ThreadUser extends Thread {

    Socket so;
    User user = null;
    Message message = null;
    boolean toSend = false;

    GenericServerRequest chain;

    public ThreadUser(Socket so) {
        this.so = so;

        chain = new LoginRequest();
    }

    @Override
    public void run() {
        try {
            ObjectOutputStream o = new ObjectOutputStream(so.getOutputStream());
            ObjectInputStream i = new ObjectInputStream(so.getInputStream());
            while (true) {
                Thread.sleep(10);
                if (toSend == true) {
                    o.writeObject(message);
                    o.flush();
                    message = null;
                    toSend = false;
                    System.out.println("THREADUSER (SERVER): Messaggio inviato all'utente " + user.getUsername());
                }
                if (so.getInputStream().available() != 0) {

                    System.out.println("THREADUSER (SERVER): Arrivano dati");
                    Message m = (Message) i.readObject();
                    m.stampa();
                    //Mettere il chain per la gestione delle risposte

                    chain.startProcessing(m, this);

                    //Significa che é arrivato un oggetto

                    /*
                    Message m = (Message) i.readObject();
                    m.stampa();
                    if ("Login".equals(m.getAction())) {
                        System.out.println("Login");
                        Session s = HibernateUtil.getSessionFactory().openSession();
                        s.beginTransaction().begin();

                        Query q = s.createQuery("from User where username = :u AND password = :p");
                        q.setParameter("u", (String) m.getParameters().get("Username"));
                        q.setParameter("p", (String) m.getParameters().get("Password"));

                        User u = (User) q.uniqueResult();
                        if (u.getUsername().equals((String) m.getParameters().get("Username")) && u.getPassword().equals((String) m.getParameters().get("Password"))) {
                            System.out.println("ricevuto");
                            Message reply = new Message();
                            reply.addReceiver(u.getUsername());
                            reply.setAction("LoginReply");
                            reply.getParameters().add(new Element("Reply", "True"));
                            Global.messages.addMessageToSend(reply);
                            setUser(u);
                        }
                        s.beginTransaction().commit();
                    }
                    if ("Rooms".equals(m.getAction())) {
                        Message reply = new Message();
                        reply.addReceiver(user.getUsername());
                        reply.setAction("RoomsReply");
                        Session s = HibernateUtil.getSessionFactory().openSession();
                        s.beginTransaction().begin();

                        Query q = s.createQuery("from Room");
                        List<Room> r = q.list();

                        for (Room ro : r) {
                            reply.getParameters().add(new Element("Room", ro.getName()));
                        }

                        s.beginTransaction().commit();
                        Global.messages.addMessageToSend(reply);
                    }

                    */
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public Socket getSo() {
        return so;
    }

    public void setSo(Socket so) {
        this.so = so;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public synchronized boolean isToSend() {
        return toSend;
    }

    public void setToSend(boolean toSend) {
        this.toSend = toSend;
    }
}
